package com.sc.nft.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.sc.nft.dao.DaoShareConfigDao;
import com.sc.nft.entity.DaoMoneyPool;
import com.sc.nft.entity.DaoShareConfig;
import com.sc.nft.entity.SharePoolWeek;
import com.sc.nft.enums.wallet.DaoWalletTypeEnum;
import com.sc.nft.service.DaoWalletService;
import com.sc.nft.service.SharePoolWeekService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author zxy
 * @since 2023-12-09 15:58:57
 * @description 商户banner
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SharePoolWeekServiceImpl implements SharePoolWeekService {
    private final DaoShareConfigDao daoShareConfigDao;
    private final DaoWalletService daoWalletService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void calShareMoney(DaoMoneyPool daoMoneyPool) {
        List<DaoShareConfig> shareConfigs = daoShareConfigDao.getOpenByDaoId(daoMoneyPool.getDaoId());
        BigDecimal sumAmount = BigDecimal.ZERO;
        for (DaoShareConfig shareConfig : shareConfigs) {
            SharePoolWeek sharePoolWeek = new SharePoolWeek();
            sharePoolWeek.setDaoId(daoMoneyPool.getDaoId());
            sharePoolWeek.setDaoPoolId(daoMoneyPool.getId());
            sharePoolWeek.setIdentityCollectionId(shareConfig.getIdentityCollectionId());
            sharePoolWeek.setPushCollectionId(shareConfig.getPushCollectionId());
            sharePoolWeek.setMarketLevel(daoMoneyPool.getMarketLevel());
            sharePoolWeek.setAwardsType(shareConfig.getAwardsType());
            switch (daoMoneyPool.getMarketLevel()) {
                case FIRST:
                    sharePoolWeek.setRatio(shareConfig.getFirstShareRatio());
                    break;
                case SECOND:
                    sharePoolWeek.setRatio(shareConfig.getSecondShareRatio());
                    break;
                case TRANS_POOL:
                    sharePoolWeek.setRatio(BigDecimal.ZERO);//目前交易池下没有比例分配 设置为0
                    break;
            }
            if (NumberUtil.equals(sharePoolWeek.getRatio(), BigDecimal.ZERO)) {
                continue;
            }
            sharePoolWeek.setSumAmount(daoMoneyPool.getAmount().multiply(sharePoolWeek.getRatio()).setScale(2, RoundingMode.FLOOR));
            sumAmount = sumAmount.add(sharePoolWeek.getSumAmount());
            sharePoolWeek.setYearWeek(daoMoneyPool.getYearWeek());
            sharePoolWeek.setIsSettle(NumberUtil.equals(sharePoolWeek.getSumAmount(), BigDecimal.ZERO));
            sharePoolWeek.insert();
        }
        daoMoneyPool.setIsSettle(true);
        daoMoneyPool.updateById();
        if (NumberUtil.isGreater(daoMoneyPool.getAmount(), sumAmount)) {
            daoWalletService.addBalance(daoMoneyPool.getDaoId(), daoMoneyPool.getId(), daoMoneyPool.getAmount().subtract(sumAmount), BigDecimal.ZERO,
                    DaoWalletTypeEnum.SHARE_RETURN, DaoWalletTypeEnum.SHARE_RETURN.getDescription()
            );
        }
    }
}